package gameSite;

import java.sql.SQLException;

import java.sql.*;
import com.mysql.jdbc.PreparedStatement;

// dfgfgdfgdsfgdgdgdgdfggdgddgddggffdgd hello matteo

public class User {

	
	int sessionID;
	int userID;
	String firstName;
	String lastName;
	String userName;
	String password;
	String address;
	String postalCode;
	String creditCard;

	private final static String findStatementString = "select * from people where id=?";
	private final static String insertStatementString = "INSERT INTO people VALUES (?,?,?,?)";
	private final static String lastIDStatement = "SELECT MAX(id) FROM people";
	
	private Connection db;

	public User(){
		
	}
	public User (int sessionid,  String username, String password1)
	{
		sessionID = sessionid;
		userName = username;
		password = password1;
		
	}
	
	public User (int sessionid, int userid,  String username, String password1, String firstname, String lastname, String address1, String postal, String creditCardNum)
	{
		sessionID = sessionid;
		userID = userid;
		userName = username;
		password = password1;
		firstName = firstname;
		lastName = lastname;
		address = address1;
		postalCode = postal;
		creditCard = creditCardNum;	
	}
	
	public User find(int id) throws ClassNotFoundException, SQLException 
	{
		JDBCdriver jdbc = new JDBCdriver();
		if (db == null)
		{
			db =jdbc.getConnectionToDatabase();
		}
		java.sql.PreparedStatement stmt = db.prepareStatement(findStatementString);
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		rs.next();
		return load(rs);
	}
	
	private User load(ResultSet rs) throws SQLException {
		int sessionID = rs.getInt(1);
		int userID = rs.getInt(2);
		String userName= rs.getString(3);
		String password = rs.getString(4);

		String firstName = rs.getString(5);
		String lastName = rs.getString(6);
		String address = rs.getString(7);
		String postalCode = rs.getString(8);
		String creditCard = rs.getString(9);
		User result = new User(sessionID, userID, userName, password, firstName, lastName, address, postalCode, creditCard );
		return result;
	}
	
	public long insert() throws ClassNotFoundException, SQLException 
	{
		JDBCdriver jdbc = new JDBCdriver();
		if (db == null)
		{
			db =jdbc.getConnectionToDatabase();
		}		
		java.sql.PreparedStatement stmt = db.prepareStatement(insertStatementString);
		
		setUserID(findNextDatabaseId());
		stmt.setInt(1, sessionID);
		stmt.setInt(2, userID);
		stmt.setString(3, userName );
		stmt.setString(4, password);
		stmt.setString(5, firstName);
		stmt.setString(6, lastName);
		stmt.setString(7, address);
		stmt.setString(8, postalCode);
		stmt.setString(9, creditCard);
		// System.out.println("in Person.insert"+ stmt.toString());
		stmt.executeUpdate();
		return getUserID();
	}
	
	
	private int findNextDatabaseId() throws ClassNotFoundException, SQLException 
	{
		JDBCdriver jdbc = new JDBCdriver();
		if (db == null)
		{
			db =jdbc.getConnectionToDatabase();
		}		
		java.sql.PreparedStatement stmt = db.prepareStatement(insertStatementString);
		ResultSet rs = stmt.executeQuery();
		rs.next();
		// System.out.println("in Person.findNextDatabaseId"+ rs.getInt(1));
		return rs.getInt(1) + 1;
}
	
	
	int  	getSessionID()		{return sessionID;}
	public int 	getUserID()			{return userID;}
	String getUserName()		{return userName;}
	String getFirstName()		{return firstName;}
	String getLastName()		{return lastName;}
	String getPassword()		{return password;}
	String getAddress()		{return address;}
	String getPostalCode()	{return postalCode;}
	String getCreditCard()	{return creditCard;}
	
	void setSessionID(int sessionIDtemp) 			{sessionID = sessionIDtemp;}
	void setUserID(int userIDtemp)				 	{userID = userIDtemp;}
	void setFirstName(String firstNametemp) 		{firstName= firstNametemp;}
	void setLastName(String lastNametemp) 		{lastName = lastNametemp;}
	void setUserName(String userNametemp) 		{userName = userNametemp;}
	void setPassword(String passwordtemp) 		{password = passwordtemp;}
	void setAddress(String addresstemp) 			{address = addresstemp;}
	void setPostalCode (String postalCodetemp) 	{postalCode = postalCodetemp;}
	void setCreditCard (String creditCardtemp ) 	{creditCard = creditCardtemp;}
	
	
	
}
